package com.sf.fy.uav.applet.temporary;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sf.fy.uav.common.enums.PlatformCodeEnum;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.entity.SigningCallbackInfo;
import com.sf.fy.uav.mapper.SigningCallbackInfoMapper;
import com.sf.fy.uav.order.entity.Feedback;
import com.sf.fy.uav.order.entity.OrderExtra;
import com.sf.fy.uav.order.entity.OrderInfo;
import com.sf.fy.uav.order.mapper.FeedbackMapper;
import com.sf.fy.uav.order.mapper.OrderExtraMapper;
import com.sf.fy.uav.order.mapper.OrderInfoMapper;
import com.sf.fy.uav.order.service.IOrderInfoService;
import com.sf.fy.uav.task.entity.Airport;
import com.sf.fy.uav.task.entity.Task;
import com.sf.fy.uav.task.mapper.AirportMapper;
import com.sf.fy.uav.task.mapper.TaskMapper;
import com.sf.fy.uav.user.entity.CityReceivingInformation;
import com.sf.fy.uav.user.entity.ReceivingInformation;
import com.sf.fy.uav.user.entity.User;
import com.sf.fy.uav.user.mapper.CityReceivingInformationMapper;
import com.sf.fy.uav.user.mapper.ReceivingInformationMapper;
import com.sf.fy.uav.user.mapper.UserMapper;
import com.sf.fy.uav.user.service.IReceivingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 01430312
 */
@RestController
@RequestMapping("/tmp/")
public class TemporaryBatchController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Autowired
    private SigningCallbackInfoMapper signingCallbackInfoMapper;

    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Autowired
    private OrderExtraMapper orderExtraMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private CityReceivingInformationMapper cityReceivingInformationMapper;

    @Autowired
    private ReceivingInformationMapper receivingInformationMapper;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private AirportMapper airportMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IReceivingInformationService iReceivingInformationService;

    @Autowired
    private PhoneEncoderUtil phoneEncoderUtil;

    @PostMapping("deal/signing-callback-info")
    public void dealSign() {
        QueryWrapper<SigningCallbackInfo> signingCallbackInfoQueryWrapper = new QueryWrapper<>();
        signingCallbackInfoQueryWrapper.ne("length(bank_num)", 0);
        Long total = signingCallbackInfoMapper.selectCount(signingCallbackInfoQueryWrapper);
        Long times = total / 1000 + 1;
        for (int i = 0; i < times; i++) {
            Page<SigningCallbackInfo> signingCallbackInfoPage = new Page<>(i, 1000);
            IPage<SigningCallbackInfo> signingCallbackInfoIPage = signingCallbackInfoMapper.selectPage(signingCallbackInfoPage, signingCallbackInfoQueryWrapper);
            List<SigningCallbackInfo> signingCallbackInfos = signingCallbackInfoIPage.getRecords();
            signingCallbackInfos.forEach(signingCallbackInfo -> {
                String bankNum = signingCallbackInfo.getBankNum();
                bankNum = phoneEncoderUtil.encryptString(bankNum);
                signingCallbackInfo.setBankNum(bankNum);
                signingCallbackInfoMapper.updateById(signingCallbackInfo);
            });

        }
    }


    @PostMapping("deal/decrypt")
    public List<String> dealDecrypt() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("create_time");
        List<User> users = userMapper.selectList(userQueryWrapper);
        List<String> returnString = new ArrayList<>();
        users.forEach(user -> {
            returnString.add(phoneEncoderUtil.decryptString(user.getPhoneNumber()));
        });
        return returnString;
    }


    @PostMapping("deal/init-app-key")
    public void initAppKey() {
        redisTemplate.opsForSet().add("RESCUE::APP_KEY::", "a774210754b8424591200ddbfb9c56c4", "cb07137b6e0d45b29f7433a6f2df74e1", "d7c446c75193987de56ec6754ef7cb82", "48495320349442a184842dfc2e485618");
    }


    @PostMapping("deal/decrypt/order")
    public List<String> dealDecryptOrder() {
        QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
        orderInfoQueryWrapper.gt("create_time", "2024-03-10");
        orderInfoQueryWrapper.eq("platform_code", PlatformCodeEnum.FY_EXPRESS.getCode());

        orderInfoQueryWrapper.orderByDesc("create_time");
        List<OrderInfo> orderInfos = orderInfoMapper.selectList(orderInfoQueryWrapper);
        List<String> returnString = new ArrayList<>();
        orderInfos.forEach(orderInfo -> {
            returnString.add(phoneEncoderUtil.decryptString(orderInfo.getSenderTel()));
        });
        System.out.println(returnString);
        return returnString;
    }
}
